icontheme: Drop unnecessary allocation
authorEmmanuele Bassi <ebassi@gnome.org>
Mon, 17 Oct 2016 10:34:36 +0000 (11:34 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Mon, 17 Oct 2016 10:44:11 +0000 (11:44 +0100)
Use a static array for the known icon sizes, now that we don't allow
registering custom icon sizes any more. This allows us to cut a one-off
allocation that makes Valgrind sad.

gtk/gtkicontheme.c

index b208b6363bb603151fbc7602bedf233a85e73679..a2255d4933099bf06c5d596e4648caa23b2b9f71 100644 (file)
@@ -5556,107 +5556,68 @@ typedef struct _IconSize IconSize;
 
 struct _IconSize
 {
-  gint size;
-  gchar *name;
-
-  gint width;
-  gint height;
-};
+  int size;
 
-typedef struct _IconAlias IconAlias;
+  const char *name;
 
-struct _IconAlias
-{
-  gchar *name;
-  gint   target;
+  int width;
+  int height;
 };
 
-static GHashTable *icon_aliases = NULL;
-static IconSize *icon_sizes = NULL;
-static gint      icon_sizes_allocated = 0;
-static gint      icon_sizes_used = 0;
-
-static void
-init_icon_sizes (void)
-{
-  if (icon_sizes == NULL)
-    {
-#define NUM_BUILTIN_SIZES 7
-      /*gint i;*/
-
-      icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
-
-      icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
-      icon_sizes_allocated = NUM_BUILTIN_SIZES;
-      icon_sizes_used = NUM_BUILTIN_SIZES;
-
-      icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
-      icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
-      icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
-      icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
-
-      /* the name strings aren't copied since we don't ever remove
-       * icon sizes, so we don't need to know whether they're static.
-       * Even if we did I suppose removing the builtin sizes would be
-       * disallowed.
-       */
-
-      icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
-      icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
-      icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
-      icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
-
-      icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
-      icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
-      icon_sizes[GTK_ICON_SIZE_BUTTON].width = 16;
-      icon_sizes[GTK_ICON_SIZE_BUTTON].height = 16;
-
-      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
-      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
-      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 16;
-      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 16;
-
-      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
-      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
-      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
-      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
-
-      icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
-      icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
-      icon_sizes[GTK_ICON_SIZE_DND].width = 32;
-      icon_sizes[GTK_ICON_SIZE_DND].height = 32;
-
-      icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
-      icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
-      icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
-      icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
-
-      g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
-
-      /* Alias everything to itself. */
-      /*i = 1; [> skip invalid size <]*/
-      /*while (i < NUM_BUILTIN_SIZES)*/
-        /*{*/
-          /*gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);*/
-
-          /*++i;*/
-        /*}*/
-
-#undef NUM_BUILTIN_SIZES
-    }
-}
+static const IconSize icon_sizes[] = {
+  [GTK_ICON_SIZE_INVALID] = {
+    .size = 0,
+    .name = NULL,
+    .width = 0,
+    .height = 0,
+  },
+  [GTK_ICON_SIZE_MENU] = {
+    .size = GTK_ICON_SIZE_MENU,
+    .name = "gtk-menu",
+    .width = 16,
+    .height = 16,
+  },
+  [GTK_ICON_SIZE_BUTTON] = {
+    .size = GTK_ICON_SIZE_BUTTON,
+    .name = "gtk-button",
+    .width = 16,
+    .height = 16,
+  },
+  [GTK_ICON_SIZE_SMALL_TOOLBAR] = {
+    .size = GTK_ICON_SIZE_SMALL_TOOLBAR,
+    .name = "gtk-small-toolbar",
+    .width = 16,
+    .height = 16,
+  },
+  [GTK_ICON_SIZE_LARGE_TOOLBAR] = {
+    .size = GTK_ICON_SIZE_LARGE_TOOLBAR,
+    .name = "gtk-large-toolbar",
+    .width = 24,
+    .height = 24,
+  },
+  [GTK_ICON_SIZE_DND] = {
+    .size = GTK_ICON_SIZE_DND,
+    .name = "gtk-dnd",
+    .width = 32,
+    .height = 32,
+  },
+  [GTK_ICON_SIZE_DIALOG] = {
+    .size = GTK_ICON_SIZE_DIALOG,
+    .name = "gtk-dialog",
+    .width = 48,
+    .height = 48,
+  },
+};
 
 static gboolean
 icon_size_lookup_intern (GtkIconSize  size,
                          gint        *widthp,
                          gint        *heightp)
 {
-  init_icon_sizes ();
-
   if (size == (GtkIconSize)-1)
     return FALSE;
 
-  if (size >= icon_sizes_used)
+  if (size >= G_N_ELEMENTS (icon_sizes))
     return FALSE;
 
   if (size == GTK_ICON_SIZE_INVALID)